<?php
/*
 * Created on Sep 25, 2007
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */

require('./View/Security.php');
require('./Model/SpecalizedInserts.php');
require('./View/ViewFunctions.php');
require('./Controller/HelperCheck.php');
if($_GET['table'] == "CellType") {
	$table = 'CellTypeID';
} elseif($_GET['table'] == "Modification") {
	$table = 'ModifiedByID';
} elseif($_GET['table'] =="Container") {
	$table = 'ContainerID';
} elseif($_GET['table'] =="Researcher") {
	$table = 'FrozenByID';
} elseif($_GET['table'] == "ContainerType") {
	$table = 'ContainerTypeID';
}
?>
<!-- needed because editing users is different than editing anything else -->
	<?php $where = (isset($_GET['edit']) ? array('id' => $_GET['itemID']) : 0) ?>
	<?php if(isset($_GET['delete'])): ?>
		<?php if($_GET['itemID'] == "new"): ?>
			Can Not remove new item
		<?php elseif($_GET['table'] == "Users"): ?>
			<?php removeUser(array("id" => $_GET['itemID'])) ?>
		<?php elseif(!deleteScript("id", $_GET['itemID'], $_GET['table'])): ?>
			You Must Remove All Instances of an Item before you Delete it.
			<a href="AdminHome.php?<?php echo $table . "=" . $_GET['itemID'] ?>">View All Instances of attempted delete</a>
		<?php else: ?>
		Operation Successful
		<?php endif; ?>
		<?php makeBackButton() ?>
		<?php die(); ?>
	<?php endif; ?>
		<!-- Queries a table depending on the choice -->
		<?php if($_GET['table'] == 'CellType'): ?>
			<?php if(isset($_GET['tableDump'])): ?>
				<?php $result = selectFromDatabase(array("Name", "Species", "id"), "CellType", $where , "ORDER BY Name ASC") ?>
			<?php else: ?>
				<?php  $result = selectFromDatabase(array("Name", "Species", "Media", "Culture", "ThawTechnique", "isPrimary", "id"), "CellType", $where , "ORDER BY Name ASC") ?>
			<?php endif; ?>
		<?php elseif($_GET['table'] == 'Modification'): ?>
			<?php $result = selectFromDatabase(array("Name", "Description", "id"), "Modification", $where , "ORDER BY Name ASC") ?>
		<?php elseif($_GET['table'] == 'Researcher'): ?>
			<?php $result = selectFromDatabase(array("Name", "Email", "Position", "id"), "Researcher",$where , "ORDER BY Name ASC") ?>
		<?php elseif($_GET['table'] == 'Container'): ?>
			<?php $result = selectFromDatabase(array("Tank", "Tower", "Position", "ContainerTypeID", "id"), "Container",$where, "ORDER BY Tank ASC") ?>
		<?php elseif($_GET['table'] == 'ContainerType'): ?>
			<?php $result = selectFromDatabase(array("numRows", "numCols", "LetterHeight", "LetterWidth", "Description", "id"), "ContainerType", $where , "ORDER BY Description ASC") ?>
		<?php elseif($_GET['table'] == 'Users'): ?>
			<?php $result = selectFromDatabase(array("User", "Admin", "pass", "id"), "Users", array('id' => $_GET['itemID'])); ?>
		<?php endif; ?>
	<!-- if table dump then uses result to display everything.  Creates wrapper form-->
	<form name="update" action="AdminHome.php" method="post" onsubmit="return validate_form(this)">
	<input type="hidden" name="table" value="<?php echo $_GET['table'] ?>">
	<input type="hidden" name="size" value="<?php echo  mysql_num_rows($result) == 0 ? 1 : mysql_num_rows($result)?>"> <!-- doesn't work yet get prper syntax -->
	<?php if(isset($_GET['tableDump'])): ?>
		<?php if($_GET['table'] == 'Users'): ?>
			<!--  Should grey out --> This option is not avalaible for editing Users. <?php makeBackButton() ?>
			<?php die(); ?>
		<?php elseif($_GET['table'] == 'Container'): ?>
			This option is not avalaible for editing Containers. <?php makeBackButton() ?>
			<?php die(); ?>
		<?php elseif($_GET['table'] == 'ContainerType'): ?> 
			This option is not avalaible for editing Container Types <?php makeBackButton() ?>
			<?php die(); ?>
		<?php endif; ?>
		<?php require('./View/addOrEditView.php'); ?>
	<?php else: ?> <!-- Else one item selected -->
		<?php if ($_GET['itemID'] != "new" && $_GET['table'] == 'ContainerType'): ?>
			It is only possible to create new container types, not edit existing ones <?php makeBackButton() ?>
			<?php die(); ?>
		<?php endif; ?>
		<!--  //Making a line here saves 6 lines of code in each of the edit functions.  This probably won't work with make header -->
		<?php $line= mysql_fetch_array($result, MYSQL_ASSOC);?> 
		<input type="hidden" name="1[id]" value="<?php echo $line['id'] ?>">
		<?php require('./View/Edit' .$_GET['table'] . '.php') ?>
		<input type="submit" name="update" value="Update Table">
		</table>
		</form>
	<?php endif; ?>